package co.edu.uniandes.sar.beans;

import co.edu.uniandes.sar.entidades.Requerimiento;
import co.edu.uniandes.sar.beans.util.JsfUtil;
import co.edu.uniandes.sar.beans.util.PaginationHelper;
import co.edu.uniandes.sar.ejbs.RequerimientoFacade;

import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

@ManagedBean(name = "requerimientoBean")
@SessionScoped
public class RequerimientoBean implements Serializable {

    private Requerimiento current;
    @EJB
    private co.edu.uniandes.sar.ejbs.RequerimientoFacade ejbFacade;
    private PaginationHelper pagination;
    private int selectedItemIndex;

    public RequerimientoBean() {
    }

    public Requerimiento getSelected() {
        if (current == null) {
            current = new Requerimiento();
            selectedItemIndex = -1;
        }
        return current;
    }

    private RequerimientoFacade getFacade() {
        return ejbFacade;
    }

    public PaginationHelper getPagination() {
        if (pagination == null) {
            pagination = new PaginationHelper(10) {

                @Override
                public int getItemsCount() {
                    return getFacade().count();
                }
                @Override
                public DataModel createPageDataModel() {
                    return new ListDataModel(getFacade().findRange(new int[]{getPageFirstItem(), getPageFirstItem() + getPageSize()}));
                }
            };
        }
        return pagination;
    }

    public String prepararNuevo() {
        current = new Requerimiento();
        selectedItemIndex = -1;
        return "requerimiento/nuevoRequerimiento";
    }

    public String crear() {
        try {
            getFacade().create(current);
            JsfUtil.addSuccessMessage("Se ha enviado correctamente el Requerimiento SAR"+current.getIdRequerimiento());
            return null;
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, "Se produjo un error al intentar guardar");
            return null;
        }
    }

    @FacesConverter(forClass = Requerimiento.class)
    public static class RequerimientoBeanConverter implements Converter {

        public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
            if (value == null || value.length() == 0) {
                return null;
            }
            RequerimientoBean controller = (RequerimientoBean) facesContext.getApplication().getELResolver().
                    getValue(facesContext.getELContext(), null, "requerimientoBean");
            return controller.ejbFacade.find(getKey(value));
        }

        java.lang.Integer getKey(String value) {
            java.lang.Integer key;
            key = Integer.valueOf(value);
            return key;
        }

        String getStringKey(java.lang.Integer value) {
            StringBuffer sb = new StringBuffer();
            sb.append(value);
            return sb.toString();
        }

        public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
            if (object == null) {
                return null;
            }
            if (object instanceof Requerimiento) {
                Requerimiento o = (Requerimiento) object;
                return getStringKey(o.getIdRequerimiento());
            } else {
                throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + RequerimientoBean.class.getName());
            }
        }
    }
}
